// pages/home/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  // mixins: [require('../../mixin/themeChanged')],
  data: {
    inputShowed: false,
    inputSearchVal: "",
    workData: []
  },

  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    const db = wx.cloud.database()

    db.collection('workData')
      .where({
        state:1
      })
      .limit(6)
      .field({
        _id: true,
        sorting: true,
        title: true,
        tag: true,
        minContent: true,
        wage: true,
        address: true,
        createDate:true
      })
      .get({
        success: res => {
          console.log(res.data)
          this.setData({
            workData: res.data
          })
        },
      })
  },

  /**
     * 生命周期函数--监听页面初次渲染完成
     */
  onReady: function () {
    const db = wx.cloud.database()

    if (wx.getStorageSync('identityStatu') == '') {
      var _openid = wx.getStorageSync('userOpenid').OPENID

      // 求职者
      db.collection('usersData').where({
        _openid: _openid
      }).get({
        success: function (res) {
          console.log(res.data)
          if (res.data.length == 0) {
            db.collection('shopmanData').where({
              _openid: _openid
            }).get({
              success: function (res) {
                console.log(res.data)
                if (res.data.length == 0) {
                  wx.navigateTo({
                    url: "../registered/registered"
                  })
                } else {
                  wx.setStorageSync('identityStatu', 2)
                }
              }
            })
          } else {
            wx.setStorageSync('identityStatu', 1)
          }
        }
      })
    }
  },


  bindUrlToWorkContent: function (e) {
    wx.navigateTo({
      url: '../workContent/workContent',
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('workDataId', {
          data: e.currentTarget.dataset.openid
        })
      }
    })
  },

  bindUrlToSearch: function () {
    const inputSearchVal = this.data.inputSearchVal;
    wx.navigateTo({
      url: '../search/search',
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('search', {
          data: inputSearchVal
        })
      }
    })
  },

  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputSearchVal: "",
      inputShowed: false
    });
  },
  clearInput: function () {
    this.setData({
      inputSearchVal: ""
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputSearchVal: e.detail.value
    });
  },

  InsertWordData: function () {
    const db = wx.cloud.database()
    try {
      db.collection('workData').add({
        data: {
          sorting: 3,
          title: "口腔助理医生",
          tag: [{
            tagData: "包住包吃",
            tagColor: "green"
          }, {
            tagData: "定期体检",
            tagColor: "green"
          }, {
            tagData: "带薪年假",
            tagColor: "green"
          }],
          minContent: "广东茂名茂南区新福五路（即愉园中学对面）",
          wage: '4500-6000元/月',
          address: '广东茂名茂南区新福五路（即愉园中学对面）',
          workInformation: "口腔助理医生职位描述：有口腔助理医师资格证，精通口腔全科操作，熟练掌握修复、牙周病、正畸，等技术能力者优先，良好的服务意识和沟通协调能力",
          workrequirements: "职位类型：医疗 发布时间：2021-09-17 有效日期：2021-10-17 基本要求：年龄不限 工作地点： 茂名 茂南区"
        },
        success: function (res) {
          console.log(res)
        }
      })
    } catch (e) {
      console.error(e);
    }

  },

  butten: function () {
    console.log(this.data.workData)
  }
})